package in.muton.edushare.errorPages;

import in.muton.edushare.EduSharePage;
import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.FooterLinks;
import in.muton.edushare.panels.userfeedback.FeedbackFormPanel;
import in.muton.edushare.url.URLLoader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.EmptyPanel;

import com.edushare.common.constants.URLConstants;

public class InternalErrorPage extends EduSharePage {

	private static Log log = LogFactory.getLog(InternalErrorPage.class);
	private boolean formOpen = false;
	
	public InternalErrorPage() {
		log.info("REDIRECTING TO INTERNAL ERROR PAGE");
		
		try{
//			EduShareSession.get().invalidate();
			
			BookmarkablePageLink<Void> headerLoginLink = 
				new BookmarkablePageLink<Void>("loginLink", URLLoader.getPageClass(URLConstants.LOGIN_PATH));
			if(EduShareSession.get().isLoggedIn()){
				headerLoginLink.add(new Label("loginTxt", "Go back to Home"));
			}else{
				headerLoginLink.add(new Label("loginTxt", "Login"));
			}
			add(headerLoginLink);
			
			FeedbackFormPanel ffp = new FeedbackFormPanel("feedbackform"){
				@Override
				public void afterSubmission() {
					formOpen = true;
				}
				
				@Override
				protected void onBeforeRender() {
					if(InternalErrorPage.this.formOpen)
						add(new SimpleAttributeModifier("style", "display:block;"));
					else
						add(new SimpleAttributeModifier("style", "display:none;"));
					super.onBeforeRender();
				}
				
			};
			ffp.setOutputMarkupId(true);
			ffp.setMarkupId("feedbackform");
			add(ffp);
			add(new FooterLinks("footerLinks"));
		}catch(Exception e){
			log.fatal("Error while displaying Error Page", e);
			try{
				add(new EmptyPanel("feedbackform"));
			}catch(Exception e1){
				//ignore
			}
		}
	}

}
